package com.issac.service.serviceImpl;

import com.issac.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @ClassName UserServiceImpl
 * @Author zy
 * @Date 2021/11/15 19:55
 * @Description UserServiceImpl
 * @Version 1.0
 */
@Service
public class UserServiceImpl implements UserDetailsService {
    @Autowired
    private PasswordEncoder encoder;

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String accountId) throws UsernameNotFoundException {

        com.issac.entity.User user = userMapper.selectById(accountId);

        if (user == null){
            throw new UsernameNotFoundException("用户名不存在");
        }

        // 把查询出来的密码（注册时已经加密过）进行解析，然后将其放入构造方法中
        String password = encoder.encode(user.getPassword());
        return new User(accountId,password,
                AuthorityUtils.commaSeparatedStringToAuthorityList(""));
    }
}
